import Mock from 'mockjs';
import ls from '../utils/localStorage';

// Mock.Random 用来生成各种随机数据
const Random = Mock.Random

const localArticles = ls.getItem('articles')
const localUser = ls.getItem('user')
const localUserName = localUser ? localUser.name : undefined

let nextArticleId = 1

if (Array.isArray(localArticles) && localArticles.length) {
    nextArticleId = parseInt(localArticles[localArticles.length - 1].articleId) + 1
}

// 生成测试文章
export const mockArticles = (num = 10) => {
    const forbiddenNames = [localUserName, 'topics']

    let articles = []
    let unames = []

    // 限制生成数量，因为 localStorage 有储存限制
    num = num > 60 ? 60 : num

    // 生成测试文章，不包含点赞用户和评论
    const arr = [...Array(num)].forEach((article, index) => {
        // 随机生成英文名
        let uname = Random.first()
        // 生成的英文名已存在，则重新生成
        // indexOf()方法返回在数组中可以找到一个给定元素的第一个索引，如果不存在，则返回-1
        while (unames.indexOf(uname) !== -1 || forbiddenNames.indexOf(uname) !== -1) {
            uname = Random.first()
        }

        unames.push(uname)
        // 随机文章
        articles.push({
            uid: index + 2,
            articleId: nextArticleId + index,
            // 随机生成 10 - 20 字的中文标题
            title: Random.ctitle(10, 20),
            // 随机生成 3 - 5 个的中文段落
            content: Random.cparagraph(3, 5),
            date: new Date(),
            likeUsers: [],
            comments: [],
            uname
        })
    })

    // 在测试文章中加入点赞用户和评论
    articles.forEach((article) => {
        let likeUsers = []
        let comments = []
        let randomArticles = getRandomArticles(articles)

        // 加入点赞用户
        randomArticles.forEach((article) => {
            likeUsers.push({
                uid: article.uid,
                uname: article.uname
            })
        })

        randomArticles = getRandomArticles(articles)
        // 加入评论
        randomArticles.forEach((article, index) => {
            comments.push({
                uid: article.uid,
                commentId: index + 1,
                // 随机生成 5 - 10 字的中文文本
                content: Random.csentence(5, 10),
                date: article.date,
                uname: article.uname
            })
        })

        article.likeUsers = likeUsers
        article.comments = comments
    })

    // 返回测试文章
    return articles
}


// 获取指定数量的随机的不重复文章
function getRandomArticles(articles, num = 5) {
    const randomNum = Math.floor(Math.random() * num + 1)
    const randomArticles = [...Array(randomNum)].map(() => articles[Math.floor(Math.random() * articles.length)])
    // 解包通过set类型去重
    return [...new Set(randomArticles)]

}